package cn.tannn.surenessdemo.annotation.aspect;

import cn.tannn.surenessdemo.annotation.RebuildTree;
import cn.tannn.surenessdemo.user.service.TreePathRoleMatcherService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * 刷新资源树的aop处理
 *
 * @author tnnn
 * @version V1.0
 * @date 2022-07-26 16:38
 */
@Aspect
@Component
public class RebuildTreeAspect {


    @Resource
    private TreePathRoleMatcherService treePathRoleMatcherService;

    /**
     * <p>
     *     annotation 方法级别
     * </p>
     *
     */
    @Pointcut("@annotation(cn.tannn.surenessdemo.annotation.RebuildTree)")
    public void rebuildTreePointcut() {}


    /**
     * 返回通知
     */
    @AfterReturning(value="rebuildTreePointcut()",returning="rvt")
    public void rebuildTree(JoinPoint joinPoint, Object rvt) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();
        RebuildTree tree = method.getAnnotation(RebuildTree.class);
        if (Objects.nonNull(tree)) {
            // 给redis放一份数据
            treePathRoleMatcherService.rebuildTree();
        }

    }
}
